package com.winchain.chainbill.bill.biz.transfer.rx;

import com.shell.framework.command.ChainBase;
import com.shell.framework.command.CommandContext;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.contract.cmd.ContractCancelCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillInvoiceRejectCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillMoveCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillOptCmd;
import com.winchain.chainbill.bill.biz.send.cmd.BillTimeoutCmd;
import com.winchain.chainbill.bill.biz.transfer.cmd.BillXfeCmd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillXfeRevRejectPrntSubscriber extends OnErrorNotImplementedSubscriber<BillTranEvent<CbXfe>> {
	@Autowired
	private BillTimeoutCmd<CbXfe> billTimeoutCmd;
	@Autowired
	private BillOptCmd<CbXfe> billOptCmd;
	@Autowired
	private BillXfeCmd billXfeCmd;
	@Autowired
	private BillMoveCmd<CbXfe> billMoveCmd;
	@Autowired
	private ContractCancelCmd<CbXfe> contractCancelCmd;
	@Autowired
	private BillInvoiceRejectCmd billInvoiceRejectCmd;

	public void onApplicationEvent(BillTranEvent<CbXfe> t) {
		Bill<CbXfe> source = t.getBill();

		CbOpt opt = new CbOpt();
		opt.setCurState("S006");
		opt.setOptCode("E003");
		opt.setRemark("��������������");

		t.setOperation(opt);

		CommandContext<Bill<CbXfe>> context = new CommandContext();
		context.setSource(source);
		context.setValue("Operation", t.getOperation());
		context.setValue("TimeoutOperation", t.getTimeoutOpt());

		ChainBase<Bill<CbXfe>> crb = new ChainBase();
		crb.add(this.billTimeoutCmd);
		crb.add(this.billOptCmd);
		crb.add(this.billMoveCmd);
		if (((CbXfe) source.getSource()).getXfeId() != null) {
			((CbXfe) source.getSource()).setXfeState("0");
			crb.add(this.billXfeCmd);
			crb.add(this.contractCancelCmd);
			context.setValue("invType", "3");
			context.setValue("bizId", ((CbXfe) source.getSource()).getXfeId());
			crb.add(this.billInvoiceRejectCmd);
		}
		crb.execute(context);
	}
}
